//平台消息通知
exports.platform_message = function (req, res) {

    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip * limit;

    var platformMessage = 0;
    //任务流创建
    async.waterfall([
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchPlatformNotification + "/pn_type/1" + "/skip/" + skip + "/limit/" + limit + "/sort_order/pn_create_time-desc",
                        method: 'GET',
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.platformMessageData = data.data;

                        platformMessage = data.data;
                        cb();
                    }
                );

            },
            //1.查询查看记录
            function (cb) {
                var tasks = [];
                //循环节点列表
                platformMessage.forEach(function (item, i) {
                    //创建每一个插入任务
                    tasks.push(function (cb) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchOrganizationViewMessageRecord + "/ovmr_oid/" + restfulParams.organization_id + "/ovmr_pnid/" + item.pn_id,
                                method: 'POST',
                                data: {
                                    fields: ["ovmr_id"].join(",")
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }
                                var recordData = data.data;

                                if (recordData.length > 0) {
                                    item.is_read = 1;
                                } else {
                                    item.is_read = 0;
                                }
                                cb();
                            }
                        );


                    });
                });
                async.waterfall(tasks, cb);
            },
            //1.生成查看记录
            function (cb) {
                var tasks = [];
                //循环节点列表
                platformMessage.forEach(function (item, i) {
                    //创建每一个插入任务
                    tasks.push(function (cb) {
                        if (item.is_read == 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.addOrganizationViewMessageRecord,
                                    method: 'POST',
                                    data: {
                                        ovmr_oid: restfulParams.organization_id,
                                        ovmr_pnid: item.pn_id
                                    }
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }

                                    cb();
                                }
                            );
                        } else {
                            cb()
                        }

                    });
                });
                async.waterfall(tasks, cb);
            }
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

};